package com.bear.net.httpclient.util;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.bear.cmcc.NetTestHTTP.NationalInfoActivity;
import com.bear.cmcc.NetTestHTTP.SplashScreenActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;

public class PhoneInfoUtils {
	private final static String VERIFY_SMS_CONTENT = "KTDHT";
	private final static String VERIFY_SMS_NUMBER = "1065703001";
	private final static Uri URI_SMS_INBOX = Uri.parse("content://sms/inbox");
	private final static Uri URI_SMS = Uri.parse("content://sms/");
	private final static String[] COLUMN_SMS = new String[] { "_id", "body" };

	public static boolean isStop;
	public static void getPhoneNumTry(final Context context,
			final GetPhoneNumSuccessCallback callback) {
		try{
			timerTask(callback);
			getPhoneNum(context,callback);
		}catch (Exception e) {
			e.printStackTrace();
			callback.onfaill();
		}
	}
	
	
	/**
	 * 获取本机号码，结果在callback中异步返回
	 * 
	 * @param context
	 * @param callback
	 */
	public static void getPhoneNum(final Context context,
			final GetPhoneNumSuccessCallback callback) {
		context.getContentResolver().registerContentObserver(
				Uri.parse("content://sms/"), true,
				new ContentObserver(new Handler()) {

					@Override
					public void onChange(boolean selfChange) {
						ContentResolver cr = context.getContentResolver();
						Cursor cursor = cr.query(URI_SMS_INBOX, COLUMN_SMS,
								" address=? and read = ?", new String[] {
										VERIFY_SMS_NUMBER, "0" }, "date desc");
						if (cursor != null && cursor.moveToFirst()) {
							isStop=true;
							String body = cursor.getString(cursor
									.getColumnIndex("body"));
							int id = cursor.getInt(0);
							Pattern pattern = Pattern.compile("\\d+");
							Matcher ma = pattern.matcher(body);

							if (ma.find() && callback != null) {
								callback.onSuccess(ma.group());
								ContentValues values = new ContentValues();
								values.put("read", 1);

								cr.delete(
										Uri.withAppendedPath(URI_SMS,
												String.valueOf(id)), null, null);
								cr.unregisterContentObserver(this);
							}

						}
						super.onChange(selfChange);
					}

				});

		SmsManager smsManager = SmsManager.getDefault();
		smsManager.sendTextMessage(VERIFY_SMS_NUMBER, null, VERIFY_SMS_CONTENT,
				null, null);
	}

	/**
	 * 获取手机的IMEI信息 需添加权限android.permission.READ_PHONE_STATE
	 * 
	 * @param context
	 * @return
	 */
	public static String getIMEI(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId();
	}

	/**
	 * 获取手机号成功后的回调接口
	 * 
	 * @author wangjie0503
	 * 
	 */
	public interface GetPhoneNumSuccessCallback {
		/**
		 * 成功获取手机号后执行
		 * 
		 * @param phoneNumber
		 *            手机号码
		 */
		void onSuccess(String phoneNumber);
		void onfaill();
	}

	private PhoneInfoUtils() {
	}
	
	private static Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			
		};
	};
	private static void timerTask(final GetPhoneNumSuccessCallback callback){
		handler.postDelayed(new Runnable(){
			public void run() {
				if(!isStop)
				callback.onfaill();
			}
			
		}, 10000);
	}
	
}
